package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectTypeEnum;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectBrief;
import com.jingdianjichi.subject.infra.basic.service.SubjectBriefService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class BriefSubjectTypeHandler implements SubjectTypeHandler {

    @Resource
    private SubjectBriefService subjectBriefService;
    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.getByCode(SubjectTypeEnum.BRIEF.getCode());
    }

    @Override
    public boolean add(SubjectInfoBO subjectInfoBO) {
        List<SubjectAnswerBO> subjectAnswerDTOList = subjectInfoBO.getSubjectAnswerDTOList();
        //bo To  entity
        List<SubjectBrief> subjectBriefList = new ArrayList<>();
        subjectAnswerDTOList.forEach(subjectAnswerDTO -> {
            SubjectBrief subjectBrief = new SubjectBrief();
            BeanUtils.copyProperties(subjectAnswerDTO,subjectBrief);
            subjectBriefList.add(subjectBrief);
        });
        boolean flag = subjectBriefService.saveBatch(subjectBriefList);
        return flag;
    }

    @Override
    public List<SubjectAnswerBO> query(Long subjectId) {
        List<SubjectBrief> subjectBriefList =subjectBriefService.selectBySubjectId(subjectId);
        if(subjectBriefList != null && subjectBriefList.size() > 0){
            List<SubjectAnswerBO> subjectAnswerDTOList = new ArrayList<>();
            subjectBriefList.forEach(subjectBrief -> {
                SubjectAnswerBO subjectAnswerDTO = new SubjectAnswerBO();
                BeanUtils.copyProperties(subjectBrief,subjectAnswerDTO);
                subjectAnswerDTOList.add(subjectAnswerDTO);
            });
            return subjectAnswerDTOList;
        }
        return Collections.emptyList();
    }
}
